$(function () {
    //初始化加载数据
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/product/list',
        datatype: "json",
        //数据字段一一绑定 <> 根据name
        colModel: [
            { label: 'Id', name: 'id', index: "id", width: 45, key: true,hidden:true },
            { label: '货品编码', name: 'goodsNo', index: "goods_no", width: 75 },
            { label: '货品名称', name: 'goodsName', index: "goods_name", width: 75 },
            { label: '品牌编码', name: 'brandCode', index: "brand_code", width: 75 },
            { label: '品牌名称', name: 'brandName', index: "brand_name", width: 75 },
            { label: '货品条码', name: 'goodsCode', index: "goods_code", width: 100 },
            { label: '分类编码', name: 'classifyCode', index: "classify_code", width: 75 },
            { label: '分类名称', name: 'classifyName', index: "classify_name", width: 75 },
            { label: '创建人', name: 'createUserName', index: "create_user_name", width: 75 },
            { label: '创建时间', name: 'createTime', index: "create_time", width: 95 },
            { label: '更新人', name: 'updateUserName', index: "update_user_name", width: 75 },
            { label: '更新时间', name: 'updateTime', index: "update_time", width: 95 }
        ],
        viewrecords: true,
        height: 425,
        rowNum: 10,
        rowList : [10,20,40,60,80,100,120,200],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        //读取服务器返回的json数据并解析
        jsonReader : {
            root: "data.page.list",
            page: "data.page.currPage",
            total: "data.page.totalPage",
            records: "data.page.totalCount"
        },
        //设置jqGrid将要向服务端传递的参数名称
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#pmpapp',
    data:{
        q:{
            goodsNo: null,
            goodsName: null,
            brandCode: null,
            brandName: null,
            goodsCode: null,
            classifyCode: null,
            classifyName: null
        },
        showList: true,
        title:null,
        productList:{},
        product:{
            id:null,
            goodsNo:null,
            goodsName:null,
        }
    },

    //请求方法
    methods: {

        //查询
        query: function () {
            vm.reload();
        },

        //重新load数据时的请求方法
        //重新load数据时的请求方法
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'goodsNo': vm.q.goodsNo,'goodsName': vm.q.goodsName,'brandCode': vm.q.brandCode,'brandName': vm.q.brandName,'goodsCode': vm.q.goodsCode,'classifyCode': vm.q.classifyCode,'classifyName': vm.q.classifyName},
                page:page
            }).trigger("reloadGrid");
        },

        //重置
        reset: function () {
            window.location.href=baseURL+"modules/sys/product.html";
        },

        //进入新增
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.productList = {};
            vm.product = {goodsNo:null, goodsName:null};
        },

        //进入修改
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },

        //获取详情
        getInfo: function(id){
            $.get(baseURL + "sys/product/info/"+id, function(r){
                vm.product = r.data.product;
            });
        },

        //删除
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                var data=JSON.stringify(ids);
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/product/delete",
                    contentType: "application/json",
                    data: data,
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },

        //保存-更新数据
        saveOrUpdate: function () {
            var url = vm.product.id == null ? "sys/product/save" : "sys/product/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.product),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        }

    }
});